using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        int gameNo;
        int totalScore;
        int tempScore;
        int NoOfBubbles;
        Assembly _assembly;
        Stream _imageStream;
        Random randomNo = new Random();
        System.Drawing.Color[] columnColor = new System.Drawing.Color[13];

        public Form1()
        {
            InitializeComponent();
        }


// // // // // // BEGIN OF EVENT DRIVEN FUNCTIONS // // // // //

        private void New_Click(object sender, EventArgs e)
        {
            newGame();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            gameNo = 1;
            NoOfBubbles = 0;           
        }

        public void bubbleClick(object sender, EventArgs e)
        {
            Button bubble = (Button)sender;


            // Debug Information
            //int pX, pY;
            //pX = posX(bubble.Name);
            //pY = posY(bubble.Name);
            //label1.Text = pX.ToString() + " , " + pY.ToString();
            //label1.Text += "\n Left :" + posBubble(pX - 1, pY).Name.ToString();
            //label1.Text += "\n Right :" + posBubble(pX + 1, pY).Name.ToString();
            //label1.Text += "\n Top :" + posBubble(pX, pY - 1).Name.ToString();
            //label1.Text += "\n Bottom :" + posBubble(pX, pY + 1).Name.ToString();





            // Check the action If It is "Mark" or "Burst"
            // Mark  : Group same coloured bubbles
            // Burst : Burst Grouped set of bubbles
            if (bubble.FlatAppearance.MouseOverBackColor  == System.Drawing.Color.White)
            // i.e. Mark
            {
                clearMarkings();
                markBubble(bubble);
            }
            else
            // i.e. Burst
            {
                burstBubble();
            }
        }

// // // // // // END OF EVENT DRIVEN FUNCTIONS // // // // //

// // // // // // BEGIN OF SUPPORTING FUNCTION // // // // // 

        //function to assign that returns a random color
        public System.Drawing.Color assignColor(int code)
        {
            if (code <= 20)
            {
                _imageStream = _assembly.GetManifestResourceStream("BubbleBreaker.Graphics.red1.jpeg");

                return System.Drawing.Color.Red;
            }
            else
            {
                if (code <= 40)
                {
                    _imageStream = _assembly.GetManifestResourceStream("BubbleBreaker.Graphics.blue1.jpeg");

                    return System.Drawing.Color.Blue;
                }
                else
                {
                    if (code <= 60)
                    {
                        _imageStream = _assembly.GetManifestResourceStream("BubbleBreaker.Graphics.purple1.jpeg");

                        return System.Drawing.Color.Magenta;
                    }
                    else
                    {
                        if (code <= 80)
                        {
                            _imageStream = _assembly.GetManifestResourceStream("BubbleBreaker.Graphics.green1.jpeg");

                            return System.Drawing.Color.LightGreen;
                        }
                        else
                        {
                            _imageStream = _assembly.GetManifestResourceStream("BubbleBreaker.Graphics.yellow1.jpeg");

                            return System.Drawing.Color.Yellow;
                        }
                    }
                }
            }
        }

        public int posX(string strTemp)
        {
            int pX;
            pX = Int32.Parse(strTemp.Substring(6));
            pX = pX % 11;
            if (pX == 0)
            {
                pX = 11;
            }
            //pX = pX - 1;

            return pX;
        }

        public int posY(string strTemp)
        {
            int pY;
            pY = Int32.Parse(strTemp.Substring(6));
            pY = (int)pY  / 11;
            if (posX(strTemp) > 10)
            {
                pY -= 1;
            }
            pY += 1;
            return pY;
        }

        //function to return the Bubble,
        // given its X and Y Coordinates
        public Button posBubble(int pX, int pY)
        {
            Button tempBubble = new Button();
            int bubbleNo;
            bubbleNo = ((pY - 1) * 11) + pX;
            if ((pX < 1) || (pX > 11))
            {
                tempBubble.Name = "NullX";
            }
            else
            {
                if ((pY < 1) || (pY > 12))
                {
                    tempBubble.Name = "NullY";
                }
                else
                {

                    try
                    {
                        tempBubble = (Button)this.Controls.Find("bubble" + bubbleNo.ToString(), true)[0];
                    }
                    catch
                    {
                        tempBubble.Name = "Null";
                    }
                }
            }
            return tempBubble;
        }

        // Function to Clear off 
        // markings of one set of bubbles
        // when another set is about to be marked
        public void clearMarkings()
        {
            int i;
            // int code = new System.Random().Next(100);
            Button temp = new Button();
            for (i = 1; i <= 132; i++)
            {
                temp = (Button)this.Controls.Find("bubble" + i.ToString(), false)[0];
                temp.FlatAppearance.BorderSize = 1;
                if (temp.FlatAppearance.MouseOverBackColor == System.Drawing.Color.Black)
                {

                    if (temp.BackColor == System.Drawing.Color.Red)
                    {
                        temp.BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.red1.jpeg"));
                    }
                    if (temp.BackColor == System.Drawing.Color.Blue)
                    {
                        temp.BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.blue1.jpeg"));
                    }
                    if (temp.BackColor == System.Drawing.Color.Magenta)
                    {
                        temp.BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.purple1.jpeg"));
                    }
                    if (temp.BackColor == System.Drawing.Color.LightGreen)
                    {
                        temp.BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.green1.jpeg"));
                    }
                    if (temp.BackColor == System.Drawing.Color.Yellow)
                    {
                        temp.BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.yellow1.jpeg"));
                    }

                }

                temp.FlatAppearance.MouseOverBackColor = System.Drawing.Color.White;
             
   

            }
            NoOfBubbles = 0;
            tempScore = TempScore(NoOfBubbles);
            
        }

// // // // // // END OF SUPPORTING FUNCTION // // // // // 



// // // // // BEGIN OF GAME ACTION FUNCTIONS // // // // //
     
        
        // Assigns or reassigns random color to bubbles
        public void newGame()
        {
            int i;
            

            // Reset Score Display
            lblScore.Text = "0";





            _assembly = Assembly.GetExecutingAssembly();




            // Debug Info
            // _imageStream = _assembly.GetManifestResourceStream("Form1.blue2.jpeg");
            // int temp_i;
            // for (temp_i = 0; temp_i <= _assembly.GetManifestResourceNames().GetUpperBound(); temp_i++)
            // {
            //    label1.Text += _assembly.GetManifestResourceNames[temp_i];
            // }

            System.Drawing.Image tempImage;// = new System.Drawing.Image();
            
            
            // int code = new System.Random().Next(100);
            Button temp = new Button();
            for (i = 1; i <= 132; i++)
            {
                temp = (Button)this.Controls.Find("bubble" + i.ToString(), false)[0];
                temp.FlatAppearance.BorderSize = 1;
                temp.Visible = true;
                if (gameNo == 1)
                {
                    temp.Click += new EventHandler(bubbleClick);
                }
                temp.BackColor = assignColor(randomNo.Next(100));

                Bitmap tempBitmap = new Bitmap(_imageStream);
                
                tempImage = tempBitmap;
                temp.BackgroundImage = tempImage;
                temp.FlatAppearance.MouseOverBackColor = System.Drawing.Color.White;


            }
            gameNo += 1;
        }

        //public void markBubble(Button bubble)
        //{
        //    int pX, pY;
        //    pX = posX(bubble.Name);
        //    pY = posY(bubble.Name);


        //    //Debug Info Stack
        //    //label1.Text += "\n check :" + checkBubble(bubble).ToString();
            
            
        //    // Check if same colored adajacent bubble is present
        //    if (checkBubble(bubble))
        //    {
        //        if (bubble.FlatAppearance.BorderSize < 3)
        //        {
        //            bubble.FlatAppearance.BorderSize = 5;
                    
        //            //This is to keep track of the Score
        //            NoOfBubbles++;
        //            tempScore = TempScore(NoOfBubbles);

        //            if ((posBubble(pX - 1, pY).Name.Contains("bubble")) && (posBubble(pX - 1, pY).BackColor == bubble.BackColor))
        //            {
        //                markBubble(posBubble(pX - 1, pY));
        //            }
        //            if ((posBubble(pX + 1, pY).Name.Contains("bubble")) && (posBubble(pX + 1, pY).BackColor == bubble.BackColor))
        //            {
        //                markBubble(posBubble(pX + 1, pY));
        //            }
        //            if ((posBubble(pX, pY + 1).Name.Contains("bubble")) && (posBubble(pX, pY + 1).BackColor == bubble.BackColor))
        //            {
        //                markBubble(posBubble(pX, pY + 1));
        //            }
        //            if ((posBubble(pX, pY - 1).Name.Contains("bubble")) && (posBubble(pX, pY - 1).BackColor == bubble.BackColor))
        //            {
        //                markBubble(posBubble(pX, pY - 1));
        //            }
        //        }
        //    }


        //}


        public void markBubble(Button bubble)
        {
            int pX, pY;
            pX = posX(bubble.Name);
            pY = posY(bubble.Name);


            //Debug Info Stack
            //label1.Text += "\n check :" + checkBubble(bubble).ToString();


            // Check if same colored adajacent bubble is present
            if (checkBubble(bubble))
            {
                if (bubble.FlatAppearance.MouseOverBackColor == System.Drawing.Color.White)
                {



                    bubble.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
                    if (bubble.BackColor == System.Drawing.Color.Red)
                    {
                        bubble.BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.red2.jpeg"));
                    }
                    if (bubble.BackColor == System.Drawing.Color.Blue)
                    {
                        bubble.BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.blue2.jpeg"));
                    }
                    if (bubble.BackColor == System.Drawing.Color.Yellow)
                    {
                        bubble.BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.yellow2.jpeg"));
                    }
                    if (bubble.BackColor == System.Drawing.Color.Magenta)
                    {
                        bubble.BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.purple2.jpeg"));
                    }
                    if (bubble.BackColor == System.Drawing.Color.LightGreen)
                    {
                        bubble.BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.green2.jpeg"));
                    }

                    //This is to keep track of the Score
                    NoOfBubbles++;
                    tempScore = TempScore(NoOfBubbles);

                    if ((posBubble(pX - 1, pY).Name.Contains("bubble")) && (posBubble(pX - 1, pY).BackColor == bubble.BackColor))
                    {
                        markBubble(posBubble(pX - 1, pY));
                    }
                    if ((posBubble(pX + 1, pY).Name.Contains("bubble")) && (posBubble(pX + 1, pY).BackColor == bubble.BackColor))
                    {
                        markBubble(posBubble(pX + 1, pY));
                    }
                    if ((posBubble(pX, pY + 1).Name.Contains("bubble")) && (posBubble(pX, pY + 1).BackColor == bubble.BackColor))
                    {
                        markBubble(posBubble(pX, pY + 1));
                    }
                    if ((posBubble(pX, pY - 1).Name.Contains("bubble")) && (posBubble(pX, pY - 1).BackColor == bubble.BackColor))
                    {
                        markBubble(posBubble(pX, pY - 1));
                    }
                }
            }


        }

        public void burstBubble()
        {
            int i;
            TotalScore();
            for (i = 1; i <= 12; i++)
            {
                populateColorColumn(i);
                SortColumn();
                assignColorBack(i);
            }
        }

        // Function that will return TRUE 
        // if there is a same coloured Bubble 
        // adjacent to the selected bubbel
        public bool checkBubble(Button bubble)
        {
            bool tempBool = false;
            int pX, pY;
            pX = posX(bubble.Name);
            pY = posY(bubble.Name);
            if ((posBubble(pX - 1, pY).BackColor == bubble.BackColor))
                tempBool = true;
            if ((posBubble(pX + 1, pY).BackColor == bubble.BackColor))
                tempBool = true;
            if ((posBubble(pX, pY + 1).BackColor == bubble.BackColor))
                tempBool = true;
            if ((posBubble(pX, pY - 1).BackColor == bubble.BackColor))
                tempBool = true;

            return tempBool;
        }

// // // // // END OF GAME ACTION FUNCTIONS // // // // //




        public void populateColorColumn(int colNo)
        {
            int i;
            for (i = 1; i <= 12; i++)
            {
                if (posBubble(colNo, i).FlatAppearance.MouseOverBackColor == System.Drawing.Color.White)
                {
                    if (posBubble(colNo, i).Visible)
                    {
                        columnColor[i] = posBubble(colNo, i).BackColor;
                    }
                    else
                    {
                        columnColor[i] = System.Drawing.Color.Black;
                    }
                }
                else
                {
                    // Border > 5
                    // Thus Bubble marked for deletion
                    columnColor[i] = System.Drawing.Color.Black;
                    posBubble(colNo, i).FlatAppearance.MouseOverBackColor  = System.Drawing.Color.White;

                }
            }

        }

      public void SortColumn()
        {
            int i,j,k;


            for (k = 12; k >= 1; k--)
            {
                for (i = 12; i >= 1; i--)
                {
                    for (j = 12; j >= 1; j--)
                    {
                        if (columnColor[j] == System.Drawing.Color.Black)
                        {
                            columnColor[j] = columnColor[j - 1];
                            columnColor[j - 1] = System.Drawing.Color.Black;
                        }
                    }
                }
            }

        }
            


        public void assignColorBack(int colNo)
        {
            int i;
            for (i = 1; i <= 12; i++)
            {
                if (columnColor[i] == System.Drawing.Color.Black)
                {
                    posBubble(colNo, i).Visible = false;
                }
                posBubble(colNo, i).BackColor = columnColor[i];
                if (posBubble(colNo, i).BackColor == System.Drawing.Color.Red)
                {
                    posBubble(colNo, i).BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.red1.jpeg"));
                }
                if (posBubble(colNo, i).BackColor == System.Drawing.Color.Blue)
                {
                    posBubble(colNo, i).BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.blue1.jpeg"));
                }
                if (posBubble(colNo, i).BackColor == System.Drawing.Color.Magenta)
                {
                    posBubble(colNo, i).BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.purple1.jpeg"));
                }
                if (posBubble(colNo, i).BackColor == System.Drawing.Color.LightGreen)
                {
                    posBubble(colNo, i).BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.green1.jpeg"));
                }
                if (posBubble(colNo, i).BackColor == System.Drawing.Color.Yellow)
                {
                    posBubble(colNo, i).BackgroundImage = new Bitmap(_assembly.GetManifestResourceStream("BubbleBreaker.Graphics.yellow1.jpeg"));
                }
            }
        }



        public int TempScore(int bubbleCount)
        {
            lblTempScore.Text = Convert.ToString(bubbleCount * (bubbleCount - 1));
            return (bubbleCount * (bubbleCount - 1));

        }


        public void TotalScore()
        {
                lblScore.Text = Convert.ToString(Convert.ToInt32(lblScore.Text) + Convert.ToInt32(lblTempScore.Text));


        }

    }
}